package com.zhaofei.webtong;


import android.app.Activity;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.content.Context;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.os.SystemClock;
import android.support.v4.content.FileProvider;
import android.util.Log;




import com.tencent.smtt.sdk.ValueCallback;
import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebView;

import java.io.File;



public class MyChromeWebClient extends WebChromeClient {
    private final static String TAG = "browerActivity";
    private ValueCallback<Uri> mUploadMessage;
    private ValueCallback<Uri[]> mUploadCallbackAboveL;
    private Activity mContext;

    public MyChromeWebClient(Activity activity) {
        this.mContext = activity;
    }



    @Override
    public void onProgressChanged(WebView view, int newProgress) {
        Log.i(TAG, "onProgressChanged" + newProgress);

       /* if (newProgress == 100) {

            loading.dismiss();
        } else {

        }*/

        super.onProgressChanged(view, newProgress);
    }

    // For Android 3.0-
    public void openFileChooser(ValueCallback<Uri> uploadMsg) {
        Log.d(TAG, "openFileChoose(ValueCallback<Uri> uploadMsg)");
        mUploadMessage = uploadMsg;
        /*if (videoFlag) {
            recordVideo();
        } else {
            takePhoto();
        }*/
        takePhoto();
    }

    // For Android 3.0+
    public void openFileChooser(ValueCallback uploadMsg, String acceptType) {
        Log.d(TAG, "openFileChoose( ValueCallback uploadMsg, String acceptType )");
        mUploadMessage = uploadMsg;

        takePhoto();
    }

    //For Android 4.1
    public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
        Log.d(TAG, "openFileChoose(ValueCallback<Uri> uploadMsg, String acceptType, String capture)");
        mUploadMessage = uploadMsg;

        takePhoto();
    }

    // For Android 5.0+

    public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
        mUploadCallbackAboveL = filePathCallback;
        Log.i(TAG, "onShowFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture)");

        takePhoto();

        return true;
    }

    private File fileUri = new File(Environment.getExternalStorageDirectory().getPath() + "/" + SystemClock.currentThreadTimeMillis() + ".jpg");
    private Uri imageUri;

    /**
     * 拍照
     */
    private final static int PHOTO_REQUEST = 100;

    public static final int N = 24;

    private void takePhoto() {
        imageUri = Uri.fromFile(fileUri);
        if (Build.VERSION.SDK_INT >= N) {
            imageUri = FileProvider.getUriForFile(mContext, mContext.getPackageName() + ".fileprovider", fileUri);//通过FileProvider创建一个content类型的Uri

        }
        PhotoUtils.takePicture((Activity) mContext, imageUri, PHOTO_REQUEST);
    }
}
